﻿using System;
using System.Drawing;
using System.Linq;
using ConfExpert.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using Tailord;
using Tailord.Properties;

namespace ConfExpert.Utility.Ribbon
{
    public class RibbonManager
    {
        public static int[]
               Admins = new int[] { 1,113,79 };
    
        public static bool isAdmin
        {
            get
            {
                return Admins.Contains(AppSession.UserInfo.UserId);
            }
        }


        public static bool AmI(params int[] UserIds)
        {
            return UserIds.Contains(AppSession.UserInfo.UserId);
        }

        
        public void Init()
        {
            RibbonControl.Pages.Clear();

            Image newIcon = Resources.miNew;
            Image listIcon = Resources.miTable;

            RibbonManager.Page page;
            RibbonManager.Group group;
            RibbonManager.Menu menu;
            //RibbonManager.SubMenu subMenu;

            switch (AppSession.UserInfo.UserId)
            {
                default:
                    page = AddPage(RibbonControl, "Main Page");
                    group = page.AddGroup("Core");

/*
System.ComponentModel.ComponentResourceManager rsc = new System.ComponentModel.ComponentResourceManager(GetType(MainForm));
        newItem.Glyph = CType(resources.GetObject("ButtonNew.Glyph"), System.Drawing.Image)
*/
                    if (isAdmin)
                    {
                        menu = group.AddMenu("Firma-Bayi- Kullanıcı", Resources.address_32x32, typeof(Firma));
                        menu = group.AddMenu("Müşteriler", Resources.customer_32x32, typeof(Kumas), "Customer");
                        menu = group.AddMenu("Tedarikçi Mailleri", Resources.mail_32x32, typeof(TedarikMailAdresleri));

                        menu = group.AddMenu("Kumaş", Resources.product_group_32x32, typeof(Kumas), "Kumas");
                        menu.BeginGroup();
                        menu = group.AddMenu("Kalite- Üretici", Resources.reports_32x32, typeof(Kumas), "Kalite");
                        menu = group.AddMenu("Fiyatlar", Resources.sale_item_32x32, typeof(Fiyat));
                    }

                    if (AmI(1, 113, 79, 4, 77,199,1226))
                    {
                        menu = group.AddMenu("Orders", Resources.order_item_32x32, typeof(Kumas), "Order");
                        menu = group.AddMenu("Kumaş Tedarik", Resources.FabricIcon, typeof(KumasSiparisleri));
                    }

                    if (AmI(1, 113, 79, 4, 77, 114,116,1226))
                    {
                        menu = group.AddMenu("Mağaza Siparişleri", Resources.shopfront, typeof(MagazaSiparis));
                        menu.BeginGroup();
                        menu = group.AddMenu("Mağaza Sipariş Satırları", Resources.shopfront, typeof(MagazaSiparisAyrinti));
                    }

                    if (AmI(1, 113, 79, 4, 83, 94,1226))
                    {
                        menu = group.AddMenu("Kumaş Stok", Resources.fabric, typeof(KumasStok));
                        menu.BeginGroup();

                        //menu = group.AddMenu("Kesilen Kumaşlar", Resources.FabricIcon, typeof(KesilenKumaslar));
                        //menu.SetVisibleForUsers(1, 7, 79, 4, 83, 94);
                    }
                    //if (AmI(1, 7, 79, 83, 94))
                    //{
                    //    menu = group.AddMenu("Talepten Kesim", Resources.Makas, typeof(TopKesim));
                    //    menu.BeginGroup();

                    //    menu = group.AddMenu("Manuel Kesim", Resources.ManuelBol, typeof(ManualTopBol));
                    //    menu.BeginGroup();
                    //}

                    break;
            }
        }

        #region Ribbon Manager
        RibbonControl ribbonControl;

        public RibbonControl RibbonControl
        {
            get { return ribbonControl; }
            set { ribbonControl = value; }
        }

        public RibbonManager(RibbonControl ribbonControl)
        {
            this.ribbonControl = ribbonControl;
            this.ribbonControl.TransparentEditors =
            this.ribbonControl.Manager.TransparentEditors = true;

        }

        public Page AddPage(RibbonControl Ribbon, string Caption)
        {
            RibbonPage ribbonPage = new RibbonPage(Caption);
            Page page = new Page(this, ribbonPage);
            if (Caption == "Ana Sayfa")
            {
                Ribbon.Pages.Insert(0, ribbonPage);
                Ribbon.SelectedPage = ribbonPage;
            }
            else
                Ribbon.Pages.Add(ribbonPage);

            return page;
        }

        public class Page
        {
            private RibbonManager parent;
            public RibbonManager Parent
            {
                get { return parent; }
                set { parent = value; }
            }

            private RibbonPage ribbonPage;

            public RibbonPage RibbonPage
            {
                get { return ribbonPage; }
                set { ribbonPage = value; }
            }
            public Page(RibbonManager parent, RibbonPage ribbonPage)
            {
                this.RibbonPage = ribbonPage;
                this.Parent = parent;
            }

            public Group AddGroup(string Caption)
            {
                RibbonPageGroup ribbonPageGroup = new RibbonPageGroup(Caption);
                RibbonPage.Groups.Add(ribbonPageGroup);
                ribbonPageGroup.ShowCaptionButton = false;
                return new Group(this, ribbonPageGroup);
            }
        }

        public class Group
        {
            private Page parent;
            public Page Parent
            {
                get { return parent; }
                set { parent = value; }
            }

            private RibbonPageGroup ribbonPageGroup;

            public RibbonPageGroup RibbonPageGroup
            {
                get { return ribbonPageGroup; }
                set { ribbonPageGroup = value; }
            }

            public Group(Page parent, RibbonPageGroup RibbonPageGroup)
            {
                this.RibbonPageGroup = RibbonPageGroup;
                this.Parent = parent;
            }

            public Menu AddMenu(string Caption, Bitmap icon)
            {
                BarButtonItem bbi = new BarButtonItem();
                bbi.Caption = Caption;
                bbi.RibbonStyle = DevExpress.XtraBars.Ribbon.RibbonItemStyles.Large;
                bbi.Glyph = icon;
                this.RibbonPageGroup.ItemLinks.Add(bbi);
                return new Menu(this, bbi);
            }

            public Type RelatedForm = null;
            object[] Params = null;
            public Menu AddMenu(string Caption, Bitmap icon, Type type, params object[] parameters)
            {
                RelatedForm = type;
                Params = parameters;
                Menu menu = AddMenu(Caption, icon);
                menu.BarButtonItem.ItemClick += delegate(object sender, ItemClickEventArgs e)

                { AppSession.MainForm.ShowMdiChildForm(type, parameters); };


                return new Menu(this, menu.BarButtonItem);
            }
        }

        public class Menu
        {
            private Group parent;
            public Group Parent
            {
                get { return parent; }
                set { parent = value; }
            }

            private BarButtonItem barButtonItem;
            public BarButtonItem BarButtonItem
            {
                get { return barButtonItem; }
                set { barButtonItem = value; }
            }

            public Menu(Group parent, BarButtonItem barButtonItem)
            {
                this.BarButtonItem = barButtonItem;
                this.Parent = parent;
            }

            public void AddCustomAction(Action Method)
            {
                this.BarButtonItem.ItemClick += delegate(object sender, ItemClickEventArgs e)
                {
                    Method();
                };
            }

            public SubMenu AddSubMenu(bool BeginGroup, string Caption, Image icon, Type type, params object[] parameters)
            {
                SubMenu sm = AddSubMenu(Caption, icon, type, parameters);

                foreach (BarItemLink bi in sm.BarButtonItem.Links)
                    if (bi.Item == sm.BarButtonItem)
                    {
                        bi.BeginGroup = true;
                        break;
                    }

                return sm;
            }

            public SubMenu AddSubMenu(string Caption, Image icon, Type type, params object[] parameters)
            {
                SubMenu subMenu = AddSubMenu(Caption, icon);
                subMenu.BarButtonItem.ItemClick += delegate(object sender, ItemClickEventArgs e)

                { AppSession.MainForm.ShowMdiChildForm(type, parameters); };

                return new SubMenu(this, subMenu.BarButtonItem);
            }

            public SubMenu AddSubMenu(string Caption, Image icon)
            {
                BarButtonItem.ActAsDropDown = true;
                BarButtonItem.ButtonStyle = DevExpress.XtraBars.BarButtonStyle.DropDown;
                PopupMenu popupMenu = null;
                if (BarButtonItem.DropDownControl == null)
                {
                    popupMenu = new PopupMenu(this.Parent.Parent.Parent.RibbonControl.Manager);
                    BarButtonItem.DropDownControl = popupMenu;
                }
                else
                {
                    popupMenu = (PopupMenu)BarButtonItem.DropDownControl;
                }

                BarButtonItem subMenu = new BarButtonItem();
                subMenu.Caption = Caption;
                subMenu.Glyph = icon;

                popupMenu.AddItem(subMenu);

                return new SubMenu(this, subMenu);
            }

            int[] allowedUserIds = null;
            int[] allowedDepartments = null;

            private void CheckAllowed()
            {
                bool allowed = false;

                if (allowedDepartments != null)
                    allowed = allowedDepartments.Contains(AppSession.UserInfo.DeptId);

                if (allowedUserIds != null)
                    allowed |= allowedUserIds.Contains(AppSession.UserInfo.UserId);

                if (RibbonManager.Admins.Contains(AppSession.UserInfo.UserId))
                    allowed = true;

                this.BarButtonItem.Visibility = allowed ? BarItemVisibility.Always : BarItemVisibility.Never;
                this.BarButtonItem.Links[0].Visible = allowed;
            }

            public void SetVisibleForUsers(params int[] UserIds)
            {
                allowedUserIds = UserIds;

                CheckAllowed();
            }

            public void Hide()
            {
                allowedUserIds = new int[] { };

                CheckAllowed();
            }

         

            public void BeginGroup()
            {
                foreach (BarItemLink bi in this.Parent.RibbonPageGroup.ItemLinks)
                {
                    if (bi.Item == this.BarButtonItem)
                    {
                        bi.BeginGroup = true;
                        return;
                    }
                }
            }
        }

        public class SubMenu
        {
            private Menu parent;
            public Menu Parent
            {
                get { return parent; }
                set { parent = value; }
            }

            private BarButtonItem barButtonItem;

            public BarButtonItem BarButtonItem
            {
                get { return barButtonItem; }
                set { barButtonItem = value; }
            }

            public SubMenu(Menu parent, BarButtonItem barButtonItem)
            {
                this.BarButtonItem = barButtonItem;
                this.Parent = parent;
            }

            public void AddCustomAction(Action Method)
            {
                this.BarButtonItem.ItemClick += delegate(object sender, ItemClickEventArgs e)
                {
                    Method();
                };
            }

            

            int[] allowedUserIds = null;
            int[] allowedDepartments = null;

            public void SetVisibleForUsers(params int[] UserIds)
            {
                allowedUserIds = UserIds;

                CheckAllowed();
            }

            public void Hide()
            {
                allowedUserIds = new int[] { };

                CheckAllowed();
            }

            private void CheckAllowed()
            {
                bool allowed = false;

                if (allowedDepartments != null)
                {
                    allowed = allowedDepartments.Contains(AppSession.UserInfo.DeptId);
                }

                if (allowedUserIds != null)
                {
                    allowed |= allowedUserIds.Contains(AppSession.UserInfo.UserId);
                }

                if (RibbonManager.Admins.Contains(AppSession.UserInfo.UserId))
                    allowed = true;

                if (!allowed)
                    this.BarButtonItem.Visibility = BarItemVisibility.Never;
            }

          
        }
        #endregion
    }
}